package com.my.study.structures.tree;

/**
 * @author Carlos
 * @version 1.0
 * @Description 结点
 * @date 2021/8/5 23:00
 **/
public class HeroNode {
    private int no;
    private String name;
    private HeroNode left;
    private HeroNode right;

    /** 0-左子树  1-前驱结点 */
    private int rightType;
    /** 0-右子树  1-后继结点 */
    private int leftType;


    public HeroNode(int no, String name) {
        this.no = no;
        this.name = name;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public HeroNode getLeft() {
        return left;
    }

    public void setLeft(HeroNode left) {
        this.left = left;
    }

    public HeroNode getRight() {
        return right;
    }

    public void setRight(HeroNode right) {
        this.right = right;
    }


    public int getRightType() {
        return rightType;
    }

    public void setRightType(int rightType) {
        this.rightType = rightType;
    }

    public int getLeftType() {
        return leftType;
    }

    public void setLeftType(int leftType) {
        this.leftType = leftType;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name +
                '}';
    }

    /**
     * 前序遍历
     */
    public void preOrder() {
        System.out.println(this);
        if (this.left != null) {
            this.left.preOrder();
        }
        if (this.right != null) {
            this.right.preOrder();
        }
    }

    /**
     * 中序遍历
     */
    public void infixOrder() {
        if (this.left != null) {
            this.left.infixOrder();
        }
        System.out.println(this);
        if (this.right != null) {
            this.right.infixOrder();
        }
    }

    /**
     * 后序遍历
     */
    public void postOrder() {
        if (this.left != null) {
            this.left.postOrder();
        }
        if (this.right != null) {
            this.right.postOrder();
        }
        System.out.println(this);
    }


    //==============遍历查找================

    /**
     * 前序遍历查找
     *
     * @param no 待查找的编号
     * @return HeroNode
     */
    public HeroNode preOrderSearch(int no) {
        if (this.getNo() == no) {
            return this;
        }

        HeroNode resultNode = null;
        if (this.getLeft() != null) {
            resultNode = this.left.preOrderSearch(no);
        }
        if (null != resultNode) {
            return resultNode;
        }

        if (this.getRight() != null) {
            resultNode = this.right.preOrderSearch(no);
        }

        return resultNode;
    }

    /**
     * 中序遍历
     *
     * @param no
     * @return
     */
    public HeroNode infixOrderSearch(int no) {
        HeroNode res = null;
        if (this.getLeft() != null) {
            res = this.getLeft().infixOrderSearch(no);
        }
        if (null != res) {
            return res;
        }

        if (this.no == no) {
            return this;
        }

        if (this.getRight() != null) {
            res = this.getRight().infixOrderSearch(no);
        }

        return res;
    }


    /**
     * 后序遍历
     *
     * @param no
     * @return
     */
    public HeroNode postOrderSearch(int no) {
        HeroNode res = null;

        if (this.getLeft() != null) {
            res = this.getLeft().postOrderSearch(no);
        }
        if (null != res) {
            return res;
        }

        if (this.getRight() != null) {
            res = this.getRight().postOrderSearch(no);
        }
        if (res != null) {
            return res;
        }

        if (this.no == no) {
            return this;
        }

        return null;
    }

    /**
     * 递归删除
     * @param no 待删除的编号
     */
    public void delNode(int no) {
        HeroNode node = null;
        if ((node = this.getLeft()) != null && node.getNo() == no) {
            node = null;
            return;
        }

        if ((node = this.getRight()) != null && node.getNo() == no) {
            node = null;
            return;
        }

        if (this.getLeft() != null) {
            this.getLeft().delNode(no);
        }

        if (this.getRight() != null) {
            this.getRight().delNode(no);
        }

    }

}


